﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Month
{
    /// <summary>
    /// 阶段总结
    /// </summary>
    public class AfterMonthAnalysis
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        public Benefit.Models.Sys.Operator Operator { get; set; }

        public string QueryYear { get; set; }

        public string Pdate { get; set; }

        public string DateQuery { get; set; }

        public string QueryMonth { get; set; }

        public string LastDate { get; set; }

        public Models.View.OperatorMonthSum OperatorMonth { get; set; }

        /// <summary>
        /// 上月值
        /// </summary>
        public Models.View.OperatorMonthSum OperatorLastMonth { get; set; }

        /// <summary>
        /// 去年同期
        /// </summary>
        public Models.View.OperatorMonthSum OperatorLastYearMonth { get; set; }

        /// <summary>
        /// 年均值
        /// </summary>
        public Models.View.OperatorYearSum OperatorYearSum { get; set; }

        /// <summary>
        /// 同批次交易员均值
        /// </summary>
        public Models.Month.BatchAvgMonth BatchAvgMonth { get; set; }

        /// <summary>
        /// 小组均值
        /// </summary>
        public Models.Month.GroupAvgMonth GroupAvgMonth { get; set; }

        /// <summary>
        /// 公司均值
        /// </summary>
        public Models.Month.CompanyAvgMonth CompanyAvgMonth { get; set; }

        ///// <summary>
        ///// 交易员年总计
        ///// </summary>
        //public Models.View.OperatorYearSum OperatorYearSum { get; set; }

        /// <summary>
        /// 交易员年预期
        /// </summary>
        public Models.View.OperatorYearExpected OperatorYearExpected { get; set; }

        public string JS { get; set; }



        #region 定义一些使用到的接口
        private Interface.Sys.IOperator IOperator = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.View.IOperatorYearSum IOperatorYearSum = null;
        private Interface.Month.IBatchAvgMonth IBatchAvgMonth = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.Month.IGroupAvgMonth IGroupAvgMonth = null;
        private Interface.Month.ICompanyAvgMonth ICompanyAvgMonth = null;
        private Interface.View.IOperatorYearExpected IOperatorYearExpected = null;
        #endregion


        public AfterMonthAnalysis(int queryUserId, int queryDate, DB.DBManager db)
        {
            InitInterface(db);

            this.Operator = IOperator.GetModel(queryUserId);

            this.QueryYear = queryDate.ToString().Substring(0, 4);

            this.Pdate = queryDate.ToString();

            //ViewBag.datequery = cookie.GetCookie("querydate");

            this.QueryMonth = queryDate.ToString().Substring(4, 2);

            this.LastDate = ITradeHistory.GetLastTradeHistoryDateString();

            this.OperatorMonth = IOperatorMonthSum.GetOperatorMonthSum(this.Operator.Id, Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear));

            int yyear = 0;
            int ymonth = 0;
            if (Convert.ToInt32(QueryMonth) == 1)
            {
                yyear = Convert.ToInt32(QueryYear) - 1;
                ymonth = 12;
            }
            else
            {
                yyear = Convert.ToInt32(QueryYear);
                ymonth = Convert.ToInt32(QueryMonth) - 1;
            }

            this.OperatorLastMonth = IOperatorMonthSum.GetOperatorMonthSum(this.Operator.Id, ymonth, yyear);

            this.OperatorLastYearMonth = IOperatorMonthSum.GetOperatorMonthSum(this.Operator.Id, Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear) - 1);

            this.OperatorYearSum = IOperatorYearSum.GetOperatorYearSum(this.Operator.Id, Convert.ToInt32(QueryYear));

            this.BatchAvgMonth = IBatchAvgMonth.GetBatchAvgMonth(this.Operator.BatchId, Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear));

            int groupid = IDayGroupOperators.GetGroupIdInMonth(this.Operator.Id, Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear));

            this.GroupAvgMonth = IGroupAvgMonth.GetGroupAvgMonth(groupid, Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear));

            this.CompanyAvgMonth = ICompanyAvgMonth.GetCompanyAvgMonth(Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear));

            this.OperatorYearExpected = IOperatorYearExpected.GetOperatorYearExpected(this.Operator.Id, Convert.ToInt32(QueryYear));

            var query = IOperatorMonthSum.GetOperatorDayChanges(Convert.ToInt32(QueryMonth), Convert.ToInt32(QueryYear), this.Operator.Id);

            this.JS = Benefit.Tools.ChartAtManager.ChartManager.GetMonthBenefitString(query);

            DisposeInterface();
        }


        /// <summary>
        /// 初始化接口
        /// </summary>
        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IOperator = new Service.Sys.ServiceOperator(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IOperatorYearSum = new Service.View.ServiceOperatorYearSum(db);
            IBatchAvgMonth = new Service.Month.ServiceBatchAvgMonth(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IGroupAvgMonth = new Service.Month.ServiceGroupAvgMonth(db);
            ICompanyAvgMonth = new Service.Month.ServiceCompanyAvgMonth(db);
            IOperatorYearExpected = new Service.View.ServiceOperatorYearExpected(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IOperator = null;
            ITradeHistory = null;
            IOperatorMonthSum = null;
            IOperatorYearSum = null;
            IBatchAvgMonth = null;
            IDayGroupOperators = null;
            IGroupAvgMonth = null;
            ICompanyAvgMonth = null;
            IOperatorYearExpected = null;
            #endregion
        }
    }
}
